package com.syqc.utils

import android.content.Context
import android.widget.ImageView
import com.luck.picture.lib.engine.ImageEngine
import com.luck.picture.lib.utils.ActivityCompatHelper
import com.syqc.comlib.utils.loadAlbumCover
import com.syqc.comlib.utils.loadGridImage
import com.syqc.comlib.utils.loadStringError
import com.syqc.comlib.utils.loadStringImgError
import com.syqc.comlib.utils.pauseRequest
import com.syqc.comlib.utils.resumeRequest
import com.syqc.monitor.R


/**
 * Glide4.x的加载图片引擎实现,单例模式
 * Glide4.x的缓存机制更加智能，已经达到无需配置的境界。如果使用Glide3.x，需要考虑缓存机制。
 * Created by huan on 2018/1/15.
 */
//单例模式，私有构造方法
class GlideEngines : ImageEngine {

    /**
     * 加载图片
     *
     * @param context   上下文
     * @param url       资源url
     * @param imageView 图片承载控件
     */
    override fun loadImage(context: Context?, url: String?, imageView: ImageView?) {
        if (!ActivityCompatHelper.assertValidRequest(context)) {
            return
        }
        imageView?.loadStringImgError(context!!, url!!)

    }

    override fun loadImage(
        context: Context?,
        imageView: ImageView?,
        url: String?,
        maxWidth: Int,
        maxHeight: Int
    ) {
        if (!ActivityCompatHelper.assertValidRequest(context)) {
            return
        }
        imageView?.loadStringError(context!!, url!!, maxWidth, maxHeight)

    }

    /**
     * 加载相册目录封面
     *
     * @param context   上下文
     * @param url       图片路径
     * @param imageView 承载图片ImageView
     */
    override fun loadAlbumCover(context: Context?, url: String?, imageView: ImageView?) {
        if (!ActivityCompatHelper.assertValidRequest(context)) {
            return
        }
        imageView?.loadAlbumCover(context!!, url!!, R.drawable.ps_image_placeholder)

    }


    /**
     * 加载图片列表图片
     *
     * @param context   上下文
     * @param url       图片路径
     * @param imageView 承载图片ImageView
     */
    override fun loadGridImage(context: Context?, url: String?, imageView: ImageView?) {
        if (!ActivityCompatHelper.assertValidRequest(context)) {
            return
        }
        imageView?.loadGridImage(context!!, url!!, R.drawable.ps_image_placeholder)
    }

    override fun pauseRequests(context: Context?) {
        pauseRequest(context)
    }

    override fun resumeRequests(context: Context?) {
        resumeRequest(context)
    }


    companion object {
        val instance: GlideEngines by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
            GlideEngines()
        }
    }
}